package review;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Demo2 {

}

class Grades{
    static Scanner sc = new Scanner(System.in);
    static Map<String, Map<String, Integer>> map = new HashMap<>();

    public void SetGrades(String ID) {
        System.out.println("请输入各科成绩：");
    }

    public static void main(String[] args) {
        while (true) {
            System.out.println("1.添加");
            System.out.println("2.查询");
            System.out.println("3.更新");
            System.out.println("4.删除");
            System.out.println("5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:insert();break;
                case 2:select();break;
                case 3:update();break;
                case 4:delete();break;
                case 5:return;
            }
        }
    }

    private static void insert() {
        System.out.println("请输入学号:");
        String key = sc.next();
        System.out.println("请输入科目");
        String course = sc.next();
        System.out.println("请输入成绩");
        Integer score = sc.nextInt();
        Map<String, Integer> scores;
        if (map.containsKey(key)) {
            scores = map.get(key);
            scores.put(course, score);
            map.put(key, scores);
        }else {
            scores = new HashMap<>();
            scores.put(course,score);
            map.put(key,scores);
        }
    }

    private static void select() {
        System.out.println("请输入学号:");
        String key = sc.next();
        Map<String, Integer> scores = map.get(key);
        System.out.println(key+"成绩如下：");
        System.out.println("科目"+"\t"+"成绩");
        for(String course:scores.keySet()){
            Integer score = scores.get(course);
            System.out.println(course+"\t"+score);
        }
    }

    private static void update() {
        System.out.println("请输入学号:");
        String key = sc.next();
        System.out.println("请输入科目");
        String course = sc.next();
        System.out.println("请输入更新后的成绩");
        Integer score = sc.nextInt();
        Map<String, Integer> scores = map.get(key);
        if(map.containsKey(key)){
            if(scores.containsKey(course)){
                scores.put(course,scores.get(course)+score);
            }
            else System.out.println("这个学生没有考这个课目");
        }else{
            System.out.println("没有这个学生");
        }
    }

    private static void delete() {
        System.out.println("请输入学号:");
        String key = sc.next();
        System.out.println("1.删除该学生 2.删除某个科目");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:map.remove(key);break;
            case 2:
                System.out.println("请输入科目");
                String course = sc.next();
                map.get(key).remove(course);
                break;
            default:break;
        }
    }
}